package com.ruoyi.fts.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.fts.domain.FtsGoods;
import com.ruoyi.fts.domain.FtsOrder;
import com.ruoyi.fts.domain.FtsOrderList;
import com.ruoyi.fts.domain.FtsUser;
import com.ruoyi.fts.mapper.FtsGoodsMapper;
import com.ruoyi.fts.mapper.FtsOrderListMapper;
import com.ruoyi.fts.mapper.FtsOrderMapper;
import com.ruoyi.fts.mapper.FtsUserMapper;
import com.ruoyi.fts.service.IFtsOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单管理Service业务层处理
 *
 * @author lumberer
 * @date 2024-10-15
 */
@Service
public class FtsOrderServiceImpl extends ServiceImpl<FtsOrderMapper, FtsOrder> implements IFtsOrderService {
    @Autowired
    private FtsOrderMapper ftsOrderMapper;
    @Autowired
    private FtsOrderListMapper ftsOrderListMapper;
    @Autowired
    private FtsGoodsMapper ftsGoodsMapper;
    @Autowired
    private FtsUserMapper ftsUserMapper;
    /**
     * 查询订单管理
     *
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public FtsOrder selectFtsOrderById(Long id) {
        FtsOrder ftsOrder=ftsOrderMapper.selectFtsOrderById(id);
        return ftsOrder;
    }

    /**
     * 查询订单管理列表
     *
     * @param ftsOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<FtsOrder> selectFtsOrderList(FtsOrder ftsOrder) {
        List<FtsOrder> ftsOrderList=ftsOrderMapper.selectFtsOrderList(ftsOrder);
        ftsOrderList.stream().forEach(item->{
            Long payUserId=item.getPayUserId();
            FtsUser ftsUser=ftsUserMapper.selectById(payUserId);
            item.setPayUser(ftsUser);

            Long orderId=item.getId();
            QueryWrapper<FtsOrderList> orderListQueryWrapper=new QueryWrapper<>();
            orderListQueryWrapper.eq("order_id",orderId);
            List<FtsOrderList> ftsOrderListList=ftsOrderListMapper.selectList(orderListQueryWrapper);
            item.setFtsOrderListList(ftsOrderListList);
            ftsOrderListList.stream().forEach(orderListItem->{
                FtsGoods ftsGoods =ftsGoodsMapper.selectById(orderListItem.getGoodsId());
                orderListItem.setFtsGoods(ftsGoods);
            });
        });
        return ftsOrderList;
    }

    /**
     * 新增订单管理
     *
     * @param ftsOrder 订单管理
     * @return 结果
     */
        @Transactional
    @Override
    public int insertFtsOrder(FtsOrder ftsOrder) {
                ftsOrder.setCreateTime(DateUtils.getNowDate());
            int rows = ftsOrderMapper.insert(ftsOrder);
            insertFtsOrderList(ftsOrder);
            return rows;
    }

    /**
     * 修改订单管理
     *
     * @param ftsOrder 订单管理
     * @return 结果
     */
        @Transactional
    @Override
    public int updateFtsOrder(FtsOrder ftsOrder) {
                ftsOrder.setUpdateTime(DateUtils.getNowDate());
                ftsOrderMapper.deleteFtsOrderListByOrderId(ftsOrder.getId());
            insertFtsOrderList(ftsOrder);
        return ftsOrderMapper.updateById(ftsOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
        @Transactional
    @Override
    public int deleteFtsOrderByIds(Long[] ids) {
                ftsOrderMapper.deleteFtsOrderListByOrderIds(ids);
        return ftsOrderMapper.deleteFtsOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     *
     * @param id 订单管理主键
     * @return 结果
     */
        @Transactional
    @Override
    public int deleteFtsOrderById(Long id) {
                ftsOrderMapper.deleteFtsOrderListByOrderId(id);
        return ftsOrderMapper.deleteFtsOrderById(id);
    }

        /**
         * 新增子订单信息信息
         *
         * @param ftsOrder 订单管理对象
         */
        public void insertFtsOrderList(FtsOrder ftsOrder) {
            List<FtsOrderList> ftsOrderListList = ftsOrder.getFtsOrderListList();
            Long id = ftsOrder.getId();
            if (StringUtils.isNotNull(ftsOrderListList))
            {
                List<FtsOrderList> list = new ArrayList<FtsOrderList>();
                for (FtsOrderList ftsOrderList : ftsOrderListList)
                {
                    ftsOrderList.setOrderId(id);
                    list.add(ftsOrderList);
                }
                if (list.size() > 0)
                {
                        ftsOrderMapper.batchFtsOrderList(list);
                }
            }
        }
}